local longfei = fk.CreateSkill{
  name = "wk_heg__longfei",
  tags = { Skill.Compulsory, Skill.MainPlace },
}

Fk:loadTranslationTable{
  ["wk_heg__longfei"] = "龙飞",
  -- [":wk_heg__longfei"] = "主将技，阵法技，与你处于同一队列角色的准备阶段，你观看牌堆顶X张牌并将这些牌以任意顺序置于牌堆顶或牌堆底（X为与你处于同一队列角色数）。",
  [":wk_heg__longfei"] = "主将技，阵法技，与你处于同一队列角色的准备阶段，你执行X为此队列角色数的〖观星〗效果。",

  ["@[wk_heg__longfei]"] = "",
  ["wk_heg__longfei-invoking"] = "<b><font color='red'>龙飞</font></b>"
}

local H = require "packages.ol_hegemony.util"

longfei:addEffect("arraysummon", {
  array_type = "formation",
})

local can_trigger = function(self, event, target, player, data)
  return player:hasSkill(longfei.name) and #player.room.alive_players >= 4 and H.inFormationRelation(player, target) 
    and target.phase == Player.Start-- and player:hasShownSkill(longfei.name)
end

local on_use = function(self, event, target, player, data)
  local room = player.room
  --room:notifySkillInvoked(player, longfei.name)
  --player:broadcastSkillInvoke(longfei.name)
  room:setPlayerMark(player, "wk_heg__longfei-invoking", 1)
  local num = 0
  for _, p in ipairs(room.alive_players) do
    if H.inFormationRelation(p, player) then
      num = num + 1
    end
  end
  room:askToGuanxing(player, {cards = room:getNCards(num), skill_name = longfei.name})
  room:setPlayerMark(player, "wk_heg__longfei-invoking", 0)
end

longfei:addEffect(fk.EventPhaseStart, {
  can_trigger = can_trigger,
  on_use = on_use,
})

Fk:addQmlMark({
  name = "wk_heg__longfei",
  qml_path = " ",
  how_to_show = function (name, value, player)
    local room = Fk:currentRoom()
    if #room.alive_players < 4 then return "#hidden" end
    local players = H.getFormationRelation(player)
    if #players == 0 then return "#hidden" end
    local owner
    if player:hasShownSkill(longfei.name, true, true) then
      owner = player
    else
      owner = table.find(players, function (p) return p:hasShownSkill(longfei.name, true, true) end)
    end
    if owner then
      if owner:getMark("wk_heg__longfei-invoking") > 0 and os.time() % 2 == 0 then --整个活
        return Fk:translate("wk_heg__longfei-invoking")
      end
      return Fk:translate("wk_heg__longfei")
    end
    return "#hidden"
  end,
})

longfei:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  table.forEach(room.alive_players, function (p)
    room:setPlayerMark(p, "@[wk_heg__longfei]", 1)
  end)
end)

return longfei